<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>角色表</title>
    <!-- 引入vue.js库 -->
    <script src="/js/vue.js"></script>

    <!-- element-ui引入样式 展示数据-->
    <link rel="stylesheet" href="/css/index.css"></link>
    <!-- element-ui 引入组件库 -->
    <script src="/js/index.js"></script>

    <!-- 引入axios库 从后端数据获取数据 -->
    <script src="/js/axios.min.js"></script>
</head>
<body>
<div id="RRR">

    <!-- 查询和添加操作 -->
    <el-row>
        <!-- 一行分为列   查询 添加-->
        <el-col :span="3">
            <el-input v-model="searchForm.roleName" size="mini" placeholder="请输入姓名"></el-input>
        </el-col>

        <el-col :span="3">
            <el-input v-model="searchForm.roleKey" size="mini" placeholder="请输入角色"></el-input>
        </el-col>

        <el-col :span="3">
            <el-input v-model="searchForm.createBy" size="mini" placeholder="请输入创建者"></el-input>
        </el-col>
        <el-col :span="3">
            <el-input v-model="searchForm.updateBy" size="mini" placeholder="请输入更改者"></el-input>
        </el-col>
        <el-col :span="3">
            <el-input v-model="searchForm.status" size="mini" placeholder="请输入状态"></el-input>
        </el-col>

        <el-col :span="3">
            <el-select v-model="searchForm.status" placeholder="请选职位状态" size="mini">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-col>

        <el-col :span="6">
            <el-button type="success" @click="search" icon="el-icon-search" size="mini">搜索</el-button>&nbsp;&nbsp;&nbsp;
            <el-button type="primary" @click="handleAdd" icon="el-icon-plus" size="mini">添加</el-button>&nbsp;&nbsp;&nbsp;
            <el-button type="primary" icon="el-icon-refresh" size="mini" @click="refresh">刷新</el-button>
        </el-col>


        <el-col :span="2"><div class="grid-content bg-purple"></div></el-col>
        <el-col :span="4"><div class="grid-content bg-purple-light"></div></el-col>
    </el-row>


    <!-- 页面先显示 -->
    <el-table
            :data="tableData"
            style="width: 100%">
        <!--表格的列   prop 组件   父作用域向子作用域传值-->
        <el-table-column
                prop="roleId"
                label="编号"
                width="90">
        </el-table-column>
        <el-table-column
                prop="roleName"
                label="姓名"
                width="90">
        </el-table-column>
        <el-table-column
                prop="roleKey"
                label="角色权限符号"
                width="120">
        </el-table-column>

<!--        <el-table-column-->
<!--                prop="roleSort"-->
<!--                :formatter="getRoleSort"-->
<!--                label="显示顺序"-->
<!--                width="90">-->
<!--        </el-table-column>-->
<!--        <el-table-column-->
<!--                prop="dataScope"-->
<!--                :formatter="getDataScope"-->
<!--                label="范围"-->
<!--                width="120">-->
<!--        </el-table-column>-->
        <el-table-column
                prop="status"
                :formatter="getStatus"
                label="状态"
                width="90">
        </el-table-column>
        <el-table-column
                prop="delFlag"
                :formatter="getDelFlag"
                label="删除标志"
                width="90">
        </el-table-column>
        <el-table-column
                prop="createBy"
                label="创建者"
                width="90">
        </el-table-column>
        <el-table-column
                prop="createTime"
                label="创建时间"
                width="200">
        </el-table-column>
        <el-table-column
                prop="updateBy"
                label="更新者"
                width="90">
        </el-table-column>
        <el-table-column
                prop="updateTime"
                label="更新时间"
                width="200">
        </el-table-column>
        <el-table-column
                prop="remark"
                label="备注"
                width="90">
        </el-table-column>
        <el-table-column fixed="right" label="操作"  align="center" width="300px">
            <template slot-scope="scope">
                <el-button size="mini" @click="givePerms(scope.$index, scope.row)">角色授权</el-button>
                <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">更新</el-button>
                <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>

    </el-table>

    <!-- 分页功能 -->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>

    <!-- 添加form表单 -->
    <el-dialog title="数据添加" :visible.sync="dialogFormVisible1">
        <el-form :model="form">
            <el-form-item label="姓名" :label-width="formLabelWidth">
                <el-input v-model="form.roleName" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="角色权限符号" :label-width=" formLabelWidth">
                <el-input v-model="form.roleKey" autocomplete="off"></el-input>
            </el-form-item>

            <!-- <el-form-item label="显示顺序" :label-width="formLabelWidth">
                 <el-select v-model="form.roleSort" placeholder="选择显示循序">
                     <el-option label="正序" value="1"></el-option>
                     <el-option label="倒序" value="2"></el-option>
                 </el-select>
             </el-form-item>-->

            <el-form-item label="范围" :label-width="formLabelWidth">
                <el-select v-model="form.dataScope" placeholder="请选择数据范围">
                    <el-option label="全部数据权限" value="1"></el-option>
                    <el-option label="自定义数据权限" value="2"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="角色状态" :label-width="formLabelWidth">
                <el-select v-model="form.status" placeholder="请选择角色状态">
                    <el-option label="正常" value="0"></el-option>
                    <el-option label="停用" value="1"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="该数据是否存在" :label-width="formLabelWidth">
                <el-select v-model="form.delFlag" autocomplete="off">
                    <el-option label="存在" value="0"></el-option>
                    <el-option label="删除" value="2"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="创建者" :label-width="formLabelWidth">
                <el-input v-model="form.createBy" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="创建时间" :label-width="formLabelWidth">
                <el-input v-model="form.createTime" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="更新者" :label-width="formLabelWidth">
                <el-input v-model="form.updateBy" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="更新时间" :label-width="formLabelWidth">
                <el-input v-model="form.updateTime" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="备注" :label-width="formLabelWidth">
                <el-input v-model="form.remark" autocomplete="off"></el-input>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible1 = false">取 消</el-button>
            <el-button type="primary" @click="add">确 定</el-button>
        </div>
    </el-dialog>

    <!-- 更新form表单 -->
    <el-dialog title="更新数据" :visible.sync="dialogFormVisible2">
        <el-form :model="form">
            <el-form-item label="姓名" :label-width="formLabelWidth">
                <el-input v-model="form.roleName" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="角色权限符号" :label-width="formLabelWidth">
                <el-input v-model="form.roleKey" autocomplete="off"></el-input>
            </el-form-item>

            <!-- <el-form-item label="显示顺序" :label-width="formLabelWidth">
                 <el-select v-model="form.roleSort" placeholder="选择显示循序">
                     <el-option label="正序" value="1"></el-option>
                     <el-option label="倒序" value="2"></el-option>
                 </el-select>
             </el-form-item>
 -->
            <el-form-item label="范围" :label-width="formLabelWidth">
                <el-select v-model="form.dataScope" placeholder="请选择数据范围">
                    <el-option label="全部数据权限" value="1"></el-option>
                    <el-option label="自定义数据权限" value="2"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="角色状态" :label-width="formLabelWidth">
                <el-select v-model="form.status" placeholder="请选择角色状态">
                    <el-option label="正常" value="0"></el-option>
                    <el-option label="停用" value="1"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="该数据是否存在" :label-width="formLabelWidth">
                <el-select v-model="form.delFlag" autocomplete="off">
                    <el-option label="存在" value="0"></el-option>
                    <el-option label="删除" value="2"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="创建者" :label-width="formLabelWidth">
                <el-input v-model="form.createBy" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="创建时间" :label-width="formLabelWidth">
                <el-input v-model="form.createTime" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="更新者" :label-width="formLabelWidth">
                <el-input v-model="form.updateBy" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="更新时间" :label-width="formLabelWidth">
                <el-input v-model="form.updateTime" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="备注" :label-width="formLabelWidth">
                <el-input v-model="form.remark" autocomplete="off"></el-input>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible2 = false">取 消</el-button>
            <el-button type="primary" @click="update">确 定</el-button>
        </div>
    </el-dialog>

    <!-- 删除 -->
    <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="30%">
        <span>你确定要删除吗？</span>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="deleteById">确 定</el-button>
        </span>
    </el-dialog>


    <!--角色授权弹出框-->
    <el-dialog title="权限菜单" :visible.sync="menuTreeVisible">
        <el-tree :data="data"
                 :props="defaultProps"
                 node-key="id"
                 show-checkbox
                 ref="menuTree"
                 :check-strictly="true"
                 :default-checked-keys="nodeChecked"
                 default-expand-all
        ></el-tree>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="addRoleAndMenus()">确 定</el-button>
        </div>
    </el-dialog>

</div>
<script>
    //js代码
    var vue = new Vue({
        el: "#RRR",//绑定上面DIV
        data() {//存放使用的所有属性
            return {//定义属性
                data: [],//权限树数组对象
                defaultProps: {//父对象传值
                    children: 'children',
                    label: 'label'
                },
                nodeChecked: [], //勾选项目数组对象
                menuTreeVisible: false,//权限树是否显示

                tableData: [], //表格数据
                currentPage: 1,       //页码(pageNo)  第几页
                pageSize: 10,          //默认每页显示数量
                total: 100,           //分页总数量
                searchForm: {roleName: '', roleKey: '', createBy: '', updateBy: '',status:''},//封装查询对象

                dialogFormVisible1: false,  //是否显示对话框
                dialogFormVisible2: false,  //是否显示对话框
                form: {
                    roleName: '', roleKey: '', roleSort: '',
                    dataScope: '', status: '1', delFlag: '',
                    createBy: '', createTime: '', updateBy: '',
                    updateTime: '', remark: ''
                },//form表单
                formLabelWidth: '120px',//弹出框宽度
                dialogVisible: false,
                handleClose: false,
                roleId: 0,//因为角色ID在不同的js方法种使用，定义为全局的
            }
        },


        //页面初始化时，需要执行的函数
        created: function () {
            //调用初始化方法
            this.loadData();
        },

        //存放所有使用的方法函数
        methods: {
            //定义初始化方法
            loadData() {
                //提前定义一个this，来调用vue内的属性
                var _this = this;
                //使用axios像后端发送请求
                axios.put('/role/queryByParam',//初始化请求地址
                    {
                        "pageNo": this.currentPage, "pageSize": this.pageSize,
                        data:this.searchForm
                        /*                       "data": {"roleName": this.searchForm.roleName, "roleKey": this.searchForm.roleKey,
                                                   "createBy": this.searchForm.createBy, "updateBy": this.searchForm.updateBy}//请求参数 要求JSON格式*/
                    })
                    //response获取后台数据，一定他要加data相当于从响应体body中获取
                    .then(function (response) {//回调函数
                        if (response.data.code == 200) {//说明查询正确，获取数据
                            //axios内的this相当于是axios对象，不是vue对象
                            //此时需要将取到的数据方法vue中，因此需要在外面定义一个_this
                            _this.tableData = response.data.data.list;//获取查询数据
                            _this.total = response.data.data.total;//获取查询页数的中数量
                        }
                    })
                    .catch(function (error) {//异常处理
                        console.log(error);
                    });
            },

            //刷新
            refresh() {
                //原始js重新请求
                location.reload();
            },

            //每页数量发生变化
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize = val;
                //调用方法
                this.loadData();
            },

            //页码(第几页)发生变化
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage = val;
                //调用方法
                this.loadData();
            },

            //获取部门状态 把状态数字，转为字符
            getStatus(row, column, cellValue, index){
                //cellValue status的值
                var returnValue='';
                if (cellValue==0){
                    returnValue="停用";
                }else{
                    returnValue="正常";
                }
                return returnValue;
                // return cellValue==1?"启动":"禁用";
            },
            //获取排序方式 把状态数字，转为字符
            getRoleSort(row, column, cellValue, index){
                //cellValue status的值
                var returnValue='';
                if (cellValue==1){
                    returnValue="正序";
                }else{
                    returnValue="倒序";
                }
                return returnValue;
                // return cellValue==1?"启动":"禁用";
            },
            //获取数据范围 把状态数字，转为字符
            getDataScope(row, column, cellValue, index){
                //cellValue status的值
                var returnValue='';
                if (cellValue==1){
                    returnValue="全部数据权限";
                }else{
                    returnValue="自定数据权限";
                }
                return returnValue;
                // return cellValue==1?"全部数据权限":"自定数据权限";
            },
            //获取删除标志 把状态数字，转为字符
            getDelFlag(row, column, cellValue, index){
                //cellValue status的值
                var returnValue='';
                if (cellValue==0){
                    returnValue="存在";
                }else if (cellValue==2){
                    returnValue="已删除";
                }
                return returnValue;
                // return cellValue==1?"存在":"已删除";
            },
            //获取部门状态  把状态数字，转换为字符
            // getStatus(row, column, cellValue, index) {
            //     //cellValue   status的值
            //     /* var returnValue = '';
            //      if(cellValue==1){
            //          returnValue= "启用";
            //      }else{
            //          returnValue= "禁用";
            //      }
            //      return returnValue;*/
            //     return cellValue == 1 ? "启动" : "禁用";
            // },

            //部门搜索方法
            search() {
                //直接调用重新请求，加载数据方法
                this.loadData();
            },



            //角色授权
            givePerms(index, row) {
                //弹出框，显示权限树，根据该角色对应的权限，让权限树默认勾选响应的值
                //给角色ID赋值
                this.roleId = row.roleId;
                //根据角色ID查出权限菜单ID，赋值给nodeChecked
                _this = this;
                //初始化数据,把菜单树数据加载
                axios.get('/menu/queryTreeData')
                    .then(function (response) {
                        /*   $.ajax(url:'',async:false) //默认同步
                            _this.data=tmpData;*/
                        let tmpData = response.data.data;
                        // _this.data=response.data.data;
                        //查询并给勾选项赋值
                        axios.get('/role/queryMenuIdListByRoleId?roleId=' + _this.roleId)
                            .then(function (response) {
                                _this.data = tmpData;
                                _this.nodeChecked = response.data.data;
                            });
                    });

                //显示权限菜单对话框
                this.menuTreeVisible = true;
            },


            //添加角色(职位)和权限关联
            addRoleAndMenus() {
                //获取重新选中的节点
                let nodes = this.$refs.menuTree.getCheckedNodes();
                //判断至少选中一个节点
                if (nodes.length < 1) {
                    this.$message.error("至少选中一个节点");
                    return;// 让代码终止运行
                }
                let tempMenuIds = "";
                //循环遍历
                for (var i = 0; i < nodes.length; i++) {
                    tempMenuIds += nodes[i].id + ",";
                }
                //tempMenuIds="100,101,102,200,201,"  拼接后最后加,  去掉最后一个逗号
                tempMenuIds = tempMenuIds.substr(0, tempMenuIds.length - 1);
                //tempMenuIds="100,101,102,200,201"
                alert("当前角色ID为：" + this.roleId + "，该角色对应新的权限ID为：" + tempMenuIds);
                _this = this;
                //执行后台添加方法
                axios.post('/role/addRoleAndMenu?roleId=' + this.roleId + "&menuId=" + tempMenuIds).then(function (response) {
                    console.log(response);
                    if (response.data.code == 200) {
                        //提示成功
                        _this.$message({
                            type: 'success',
                            message: '授权成功!'
                        });
                        //关闭弹框
                        _this.menuTreeVisible = false;
                        //重新加载数据
                        _this.loadData();

                    }
                }).catch(function (error) {
                    console.log(error);
                    _this.$message.error("你错了。。。");
                });
            },

            //跳转添加
            handleAdd() {
                this.dialogFormVisible1 = true;
                //清空表单
                this.form = {};
            },

            //进行添加
            add() {
                //提前定义一个this，来调用vue内的属性
                var _this = this;
                //使用axios像后端发送请求
                axios.post('/role/add',//初始化请求地址
                    this.form//请求参数 传入form表格中的数据 要求JSON格式
                )
                    //response获取后台数据，一定他要加data相当于从响应体body中获取
                    .then(function (response) {//回调函数
                        if (response.data.code == 200) {//说明查询正确，获取数据
                            //提示成功
                            _this.$message({
                                message: '恭喜你，这是一条成功消息',
                                type: 'success'
                            });
                            //关闭弹框
                            _this.dialogFormVisible1 = false;
                            //重新加载数据
                            _this.loadData();
                        }
                    })
                    .catch(function (error) {//异常处理
                        console.log(error);
                    });
            },


            //跳转更新
            handleEdit(index, row) {
                //alert(index+" "+JSON.stringify(row));
                //row={"deptName":"qqq","loc":"www","deptNo":rrr}
                this.dialogFormVisible2 = true;
                this.form = row;
                this.form.status = row.status+'';
            },

            //进行更新
            update() {
                //提前定义一个this，来调用vue内的属性
                var _this = this;
                //使用axios像后端发送请求
                axios.put('/role/update',//初始化请求地址
                    this.form//请求参数 传入form表格中的数据 要求JSON格式
                )
                    //response获取后台数据，一定他要加data相当于从响应体body中获取
                    .then(function (response) {//回调函数
                        if (response.data.code == 200) {//说明查询正确，获取数据
                            //提示成功
                            _this.$message({
                                message: '恭喜你，这是一条成功消息',
                                type: 'success'
                            });
                            //关闭弹框
                            _this.dialogFormVisible2 = false;
                            //重新加载数据
                            _this.loadData();
                        }
                    })
                    .catch(function (error) {//异常处理
                        console.log(error);
                    });
            },

            //处理删除
            handleDelete(index, row) {
                console.log(index, row);
                this.roleId = row.roleId;
                this.dialogVisible = true;
            },

            //真正删除
            deleteById() {
                console.log("123123123");
                //提前定义一个this
                var _this = this;

                //使用axios向后端发送请求
                axios.delete('/role/deleteById?roleId=' + _this.roleId,       //请求地址
                    this.form)     //请求参数  要求JSON格式
                    .then(function (response) {     //回调函数

                        //将Json对象转字符串
                        console.log(JSON.stringify(response));
                        //response获取后台数据是，一定要加data  相当于从响应body中获取
                        if (response.data.code == 200) {//说明查询正确，获取数据
                            //提示成功
                            _this.$message({
                                message: '恭喜你，这是一条成功消息',
                                type: 'success'
                            });
                            //关闭弹框
                            _this.dialogVisible = false;
                            //重新加载数据
                            _this.loadData();
                        }
                    })
                    .catch(function (error) {      //异常处理
                        console.log(error);
                    });
            },

        },
    })
</script>
</body>
</html>














